package com.yunao.service.impl;

import com.yunao.commons.constant.CookieConstant;
import com.yunao.commons.constant.RedisConstant;
import com.yunao.commons.serachcore.core.bean.*;
import com.yunao.commons.utils.CookieUtil;
import com.yunao.commons.utils.JsonUtil;
import com.yunao.dao.SubjectRepository;
import com.yunao.dao.UserRepsitory;
import com.yunao.entity.Subject;
import com.yunao.entity.User;
import com.yunao.service.IsearchService;
import com.yunao.service.SubjectService;
import com.yunao.service.report.Convert;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.stereotype.Service;

import javax.servlet.http.Cookie;
import javax.servlet.http.HttpServletRequest;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

@Service
public class SubjectServiceImpl implements SubjectService {
    @Autowired
    private SubjectRepository subjectRepository;
    @Autowired
    private UserRepsitory userRepsitory;
    @Autowired
    private StringRedisTemplate stringRedisTemplate;
    @Autowired
    private IsearchService isearchService;

    @Override
    public List<Subject> findListByUser(Integer userId) {
        return subjectRepository.findByUserId(userId);
    }

    @Override
    public Subject findOne(Integer subjectId) {
         return subjectRepository.findById(subjectId).get();
    }

    @Override
    public List<Subject> findFirstlevelBySubjectType(Integer userid,Integer parentid,Integer subjectType) {
        return subjectRepository.findByUserIdAndParentidAndSubjectType(userid,parentid,subjectType);
    }

    @Override
    public List<Subject> findByParentIdAndSubjectType(Integer parentid,Integer subjectType) {
        return subjectRepository.findByParentidAndSubjectType(parentid,subjectType);
    }

    @Override
    public List<Subject> findByUserIdAndIshome(Integer userid, Integer ishome) {
        return subjectRepository.findByUserIdAndIshome(userid,ishome);
    }

    @Override
    public List<Subject> findAlarmList(Integer userid) {
        return subjectRepository.findByUserIdAndAlarmPeriodIsNotNull(userid);
    }


    @Override
    public List<Subject> findByUserIdNeedAlarm(Integer userid, Integer needNum) {
        return subjectRepository.findByUserIdAndNeedAlarm(userid,needNum);
    }

    @Override
    public List<Subject> findByNeedAlarm(Integer need) {
        return subjectRepository.findByNeedAlarm(need);
    }


    @Override
    public Subject findByName(String subjectName) {
        return subjectRepository.findBySubjectName(subjectName);
    }

    @Override
    public Subject findByUserAndName(Integer userid,String subjectName) {
        return subjectRepository.findByUserIdAndSubjectName(userid,subjectName);
    }

    @Override
    public int findNumByOrgan(Integer organId) {
        return subjectRepository.findByOrganId(organId).size();
    }

    @Override
    public void saveByUser(Subject subject) {
        subjectRepository.save(subject);
    }

    @Override
    public void save(Subject subject) {
            subjectRepository.save(subject);
    }

    @Override
    public void delete(Integer subjectId) {
        subjectRepository.deleteById(subjectId);
    }

    @Override
    public Articles searchSubject(Integer subjectId, Map mapvalue) {
        Subject subject =null;;
        if (subjectRepository.findById(subjectId).isPresent()){
            subject =  subjectRepository.findById(subjectId).get();
        }
        Map map =subject2map(subject,mapvalue);
        System.out.println(map.toString());
        Articles articles = isearchService.query(map);
        return articles;

    }

    public static Map<String,String> subject2map(Subject subject,Map<String,String> map){
        if(!map.containsKey("keywords")){
            if (StringUtils.isEmpty(subject.getRemoveWord()))
                map.put("keywords",subject.getWord());
            else
                map.put("keywords",subject.getWord()+"-"+subject.getRemoveWord());
        }
        if (!map.containsKey("location")) {
            if (StringUtils.isNotEmpty(subject.getSubjectLocaltion()))
                map.put("location", subject.getSubjectLocaltion());
        }
        return map;
    }

    @Override
    public Articles query(Map map) {
        return isearchService.query(map);
    }

    @Override
    public Groups group(Map map) {
        return isearchService.group(map);
    }


    @Override
    public Cubes cube(Map map) {
        return isearchService.cube(map);
    }

    @Override
    public Articles distinct(Integer subjectId,Map mapvalue) {
        Subject subject =  findOne(subjectId);
//        Map map = Subject2QueryMap.subject2map(subject,mapvalue);

        return isearchService.distinct(mapvalue);
    }

    @Override
    public Wordclouds searchWordclouds(Map map) {
        return isearchService.wordcloud(map);
    }

    @Override
    public Wordrelations wordrelation(Map map) {
        return isearchService.wordrelation(map);
    }

    @Override
    public Clusters cluster(Integer subjectId) {
        Subject subject =  findOne(subjectId);
        Map map = addSubjectQuery(subject);

        return isearchService.cluster(map);
    }

    private Map addSubjectQuery(Subject subject,Map map){
//        Map map= new HashMap();
        if (subject.getRemoveWord()==null)
            map.put("keywords",subject.getWord());
        else
            map.put("keywords",subject.getWord()+"-"+subject.getRemoveWord());
        map.put("location",subject.getSubjectLocaltion());
        return map;
    }

    private Map addSubjectQuery(Subject subject){
        Map map= new HashMap();
        if (subject.getRemoveWord()==null)
            map.put("keywords",subject.getWord());
        else
            map.put("keywords",subject.getWord()+"-"+subject.getRemoveWord());
        map.put("location",subject.getSubjectLocaltion());
        return map;
    }



}
